
package org.sopt.dev.ImgKnight.monitor.chart;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Font;
import java.awt.GradientPaint;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.DateAxis;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PiePlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.category.BarRenderer;
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.data.general.DefaultPieDataset;
import org.jfree.data.time.Millisecond;
import org.jfree.data.time.TimeSeries;
import org.jfree.data.time.TimeSeriesCollection;
import org.jfree.ui.RectangleInsets;

public class FetchedResourceMeter {

private TimeSeries reserved = null;
	
	private DefaultCategoryDataset categoryDataSetForRC = null;

	private DefaultPieDataset pieDataSetForCompleteDetail = null;

	private TimeSeries fetch = null;
	
	private TimeSeries discard = null;
	
	private TimeSeries miss = null;
	
	private TimeSeries remain = null;
	
	public FetchedResourceMeter(int i) {
		categoryDataSetForRC = new DefaultCategoryDataset();
		categoryDataSetForRC.setValue(0, "Reserved", "");
		categoryDataSetForRC.setValue(0, "Complete", "");
		pieDataSetForCompleteDetail = new DefaultPieDataset();
		pieDataSetForCompleteDetail.setValue("Fetch", 0.0f);
		pieDataSetForCompleteDetail.setValue("Discard", 0.0f);
		pieDataSetForCompleteDetail.setValue("Miss", 0.0f);
		reserved = new TimeSeries("Reserved");
		reserved.setMaximumItemAge(i);
		fetch = new TimeSeries("Fetched");
		fetch.setMaximumItemAge(i);
		discard = new TimeSeries("Discard");
		discard.setMaximumItemAge(i);
		miss = new TimeSeries("Missed");
		miss.setMaximumItemAge(i);
		remain = new TimeSeries("Remain");
		remain.setMaximumItemAge(i);
	}
	
	public JFreeChart createBarChartForReservAndComplete() {
		JFreeChart chart = ChartFactory.createBarChart("Resource Fetch(2)",
				"", "Count",                
	            categoryDataSetForRC, PlotOrientation.HORIZONTAL,
	            true, true, false);
		chart.setBackgroundPaint(new Color(0xFFFFFF));
		
		CategoryPlot plot = chart.getCategoryPlot();
        
		NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
        rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
        
        BarRenderer renderer = (BarRenderer) plot.getRenderer();
        renderer.setDrawBarOutline(false);
        renderer.setMaximumBarWidth(0.10);
        
        GradientPaint gp0 = new GradientPaint(
            0.0f, 0.0f, Color.blue, 0.0f, 0.0f, Color.lightGray);
        GradientPaint gp1 = new GradientPaint(
            0.0f, 0.0f, Color.green,0.0f, 0.0f, Color.lightGray);    
        renderer.setSeriesPaint(0, gp0);
        renderer.setSeriesPaint(1, gp1);       
        return chart;
	}
	
	public JFreeChart createPieChartForCompleteDetail() {
		JFreeChart jfreechart = ChartFactory.createPieChart("Resource Fetch(3)", 
				pieDataSetForCompleteDetail, false, true, false);
		PiePlot plot = (PiePlot)jfreechart.getPlot();
		plot.setLabelFont(new Font("SansSerif", 1, 10));
		plot.setNoDataMessage("No data available");
		plot.setCircular(false);
        plot.setLabelGap(0.01);
        jfreechart.setBorderPaint(Color.white);
		jfreechart.setBackgroundPaint(Color.white);
		return jfreechart;
	}
	
	public JFreeChart createLineChart() {
		TimeSeriesCollection timeseriescollection = new TimeSeriesCollection();
		//timeseriescollection.addSeries(reserved);
		timeseriescollection.addSeries(fetch);
		//timeseriescollection.addSeries(discard);
		//timeseriescollection.addSeries(miss);
		//timeseriescollection.addSeries(remain);
		
		DateAxis dateaxis = new DateAxis("Time");
		NumberAxis numberaxis = new NumberAxis("Count");
		dateaxis.setTickLabelFont(new Font("SansSerif", 0, 12));
		numberaxis.setTickLabelFont(new Font("SansSerif", 0, 12));
		dateaxis.setLabelFont(new Font("SansSerif", 0, 14));
		numberaxis.setLabelFont(new Font("SansSerif", 0, 14));
		numberaxis.setAutoRangeIncludesZero(false);
		
		XYLineAndShapeRenderer xylineandshaperenderer = new XYLineAndShapeRenderer(true, false);
		xylineandshaperenderer.setSeriesPaint(0, Color.blue);
		/*xylineandshaperenderer.setSeriesPaint(0, Color.red);
		xylineandshaperenderer.setSeriesPaint(1, Color.green);
		xylineandshaperenderer.setSeriesPaint(2, Color.blue);
		xylineandshaperenderer.setSeriesPaint(3, Color.cyan);
		xylineandshaperenderer.setSeriesPaint(4, Color.orange);*/
		xylineandshaperenderer.setBaseStroke(new BasicStroke(3F, 0, 2));
      
		XYPlot xyplot = new XYPlot(timeseriescollection, dateaxis, numberaxis, xylineandshaperenderer);
		xyplot.setBackgroundPaint(Color.lightGray);
		xyplot.setDomainGridlinePaint(Color.white);
		xyplot.setRangeGridlinePaint(Color.white);
		xyplot.setAxisOffset(new RectangleInsets(5D, 5D, 5D, 5D));
		xyplot.setNoDataMessage("No data available");
		
		dateaxis.setAutoRange(true);
		dateaxis.setLowerMargin(0.0D);
		dateaxis.setUpperMargin(0.0D);
		dateaxis.setTickLabelsVisible(true);
		numberaxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
      
		JFreeChart jfreechart = new JFreeChart("Resource Fetch", new Font("SansSerif", 1, 24), xyplot, true);
		jfreechart.setBackgroundPaint(Color.white);
      
		return jfreechart;
	}
	
	public void setBarChartForRCDataReserved(double value) {
		categoryDataSetForRC.setValue(value, "Reserved", "");
	}
	
	public void setBarChartForRCDataCompleted(double value) {
		categoryDataSetForRC.setValue(value, "Complete", "");
	}
	
	public void setPieChartForCompleteDetailData(String key, double value) {
		pieDataSetForCompleteDetail.setValue(key, value);
	}
	
	public void addReservedMemPushObservation(final double d) {
		reserved.addOrUpdate(new Millisecond(), d);
	}
	
	public void addFetchObservation(final double d) {
		fetch.addOrUpdate(new Millisecond(), d);
	}
	
	public void addDiscardObservation(final double d) {
		discard.addOrUpdate(new Millisecond(), d);
	}
	
	public void addMissObservation(final double d) {
		miss.addOrUpdate(new Millisecond(), d);
	}
	
	public void addRemainObservation(final double d) {
		remain.addOrUpdate(new Millisecond(), d);
	}
}
